package com.test.uiapp2.function.Music;

import android.os.RemoteException;
import android.util.Log;

import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;

import com.test.uiapp2.base.TestBase;
import com.test.uiapp2.element.Base.BaseElement;
import com.test.uiapp2.element.Music.MusicElement;

import org.junit.Test;

import java.io.IOException;

public class MusicFunction extends TestBase {

    public  void PreviousMusic(){
        try {
            startApp(MusicElement.MUSIC);
            sleep(WAIT);
            downStatusBar();
            UiObject musicbutton = getId(BaseElement.MUSIC, MusicElement.TOGGLE_BTN);
            if(musicbutton.exists()){
                musicbutton.click();
                sleep(MWAITTIME);
                String musicname = getId(BaseElement.MUSIC, MusicElement.TRACKNAME).getText();
                Log.i(TAG, "当前歌曲名称="+musicname);
                UiObject pre_btn = getId(BaseElement.MUSIC, MusicElement.PRE_BTN);
                pre_btn.click();
                sleep(MWAITTIME);
                String PreviousMusicName = getId(BaseElement.MUSIC, MusicElement.TRACKNAME).getText();
                Log.i(TAG, "上一首歌曲名称="+PreviousMusicName);
                verify("未切换成功歌曲",!musicname.equals(PreviousMusicName));
                sleep(WAIT);
                upwardStatusBar();
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void NextMusic(){
        try {
//            startApp(MusicElement.MUSIC);
            sleep(WAIT);
            downStatusBar();
            UiObject musicbutton = getId(BaseElement.MUSIC, MusicElement.TOGGLE_BTN);
            if(musicbutton.exists()){
                musicbutton.click();
                sleep(MWAITTIME);
                String musicname = getId(BaseElement.MUSIC, MusicElement.TRACKNAME).getText();
                Log.i(TAG, "当前歌曲名称="+musicname);
                UiObject next_btn = getId(BaseElement.MUSIC, MusicElement.NEXT_BTN);
                next_btn.click();
                sleep(MWAITTIME);
                String PreviousMusicName = getId(BaseElement.MUSIC, MusicElement.TRACKNAME).getText();
                Log.i(TAG, "下一首歌曲名称="+PreviousMusicName);
                verify("未切换成功歌曲",!musicname.equals(PreviousMusicName));
                sleep(WAIT);
                upwardStatusBar();
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }
    //熄屏-解锁屏幕
    public void  UnlockScreen(){
        try {
            uiDevice.executeShellCommand("input keyevent 26");
            sleep(MWAITTIME);
            if (!uiDevice.isScreenOn()){
                Log.i(TAG,"点亮屏幕");
                uiDevice.wakeUp();
                if(getId(BaseElement.LOCK_ICON).exists()){
                    try {
                        sleep(WAIT);
                        testLOCK();
                    } catch (UiObjectNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }


    //向下滑出通知栏
    public void downStatusBar(){
        sleep(WAIT);
        uiDevice.swipe(506,15,574,791,STEP);
    }
    //向上滑动关闭通知栏
    public void upwardStatusBar(){
        sleep(WAIT);
        uiDevice.swipe(593,1712,640,685,STEP);
    }




}
